package top.oool.autoconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.oool.service.IPrintService;
import top.oool.service.PrintLogVo;
import top.oool.service.PrintService;

import javax.annotation.Resource;

/**
 * Created by IntelliJ IDEA.
 * User: kui.zhang
 * Date: 2019/4/22 13:14
 * Email:kui.zhang@chinaredstar.com
 */

/**
 * @ConditionalOnBean：当SpringIoc容器内存在指定Bean的条件
 * @ConditionalOnClass：当SpringIoc容器内存在指定Class的条件
 * @ConditionalOnExpression：基于SpEL表达式作为判断条件
 * @ConditionalOnJava：基于JVM版本作为判断条件
 * @ConditionalOnJndi：在JNDI存在时查找指定的位置
 * @ConditionalOnMissingBean：当SpringIoc容器内不存在指定Bean的条件
 * @ConditionalOnMissingClass：当SpringIoc容器内不存在指定Class的条件
 * @ConditionalOnNotWebApplication：当前项目不是Web项目的条件
 * @ConditionalOnProperty：指定的属性是否有指定的值
 * @ConditionalOnResource：类路径是否有指定的值
 * @ConditionalOnSingleCandidate：当指定Bean在SpringIoc容器内只有一个，或者虽然有多个但是指定首选的Bean
 * @ConditionalOnWebApplication：当前项目是Web项目的条件
 */
@Configuration
@EnableConfigurationProperties(OoolRestProperties.class)
public class OoolRestAutoConfigure {
    private final OoolRestProperties ooolRestProperties;

    public OoolRestAutoConfigure(OoolRestProperties ooolRestProperties) {
        this.ooolRestProperties = ooolRestProperties;
    }

    @Bean
    @ConditionalOnMissingBean(IPrintService.class)
    @ConditionalOnProperty(name = "spring.boot.oool.enabled", havingValue = "true")
    public IPrintService getPrintService() {
        IPrintService printService = new PrintService();
        printService.print(new PrintLogVo(ooolRestProperties.getLogName(), ooolRestProperties.getLogValue(), ooolRestProperties.getLogId()));
        return printService;
    }
}
